﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace DomainInfoLib
{
    public class WanWangAPI:IWebAPI
    {
        private static bool? ProcessAvaliableStr(string avaliableStr)
        {
            if (string.IsNullOrEmpty(avaliableStr))
            {
                return null;
            }

            string key = avaliableStr.Substring(0, 3);

            if (key == "210")
            {
                return true;
            }

            return false;
        }

        static XmlDocument xdoc = new XmlDocument();

        private static string ProcessXmlStr(string XmlStr)
        {
            try
            {
                if (string.IsNullOrEmpty(XmlStr)) return "";

                xdoc.LoadXml(XmlStr);

                XmlNode xn = xdoc.ChildNodes[1].ChildNodes[2];

                return xn.InnerText;
            }
            catch
            {

                return "";
            }



        }


        public RegisterInfo GetResponseByWebAPI(string DomainName)
        {
            string url = "http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + DomainName;

            string xmlstr = BaseInfo.CallWebPage(url, null, null);

            string avaliableStr = ProcessXmlStr(xmlstr);

            bool? Avaliable = ProcessAvaliableStr(avaliableStr);

            if (!Avaliable.HasValue)
            {
                return null;
            }

            return RegisterInfo.BuildRegisterInfo(DomainName, xmlstr, Avaliable, this.GetType().Name);
        }


    }
}
